package org.pictual.business.dao;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import org.apache.log4j.Logger;

public class ServiceLocator {

	private static ServiceLocator _instance;
	InitialContext initialcontext;
	protected static Logger log= Logger.getLogger(ServiceLocator.class);
	
    private ServiceLocator() {}
 
    public static ServiceLocator getInstance() {
    	if(_instance==null){
    		_instance = new ServiceLocator();
    	}
        return _instance;
    }
    
    public DataSource getDatasource(String dataSourceName) throws NamingException{
    	initialcontext = new InitialContext();
		Context initContext = initialcontext;
		Context envContext  = (Context)initContext.lookup("java:/comp/env");
		DataSource dataSource = null;
		try {
			return dataSource = (DataSource) envContext.lookup(dataSourceName);
		} catch (NamingException e) {
			log.error("ServiceLocator.getDatasource(String dataSourceName): NamingException ", e);
		}
		return dataSource;	
    }
}
